package com.ruoyi.develop.factory.factoryTestMachineryLink.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.factory.factoryProduceWork.domain.FactoryProduceWork;
import com.ruoyi.develop.factory.factoryProduceWork.mapper.FactoryProduceWorkMapper;
import com.ruoyi.develop.factory.factoryTestMachineryStick.domain.FactoryTestMachineryStick;
import com.ruoyi.develop.factory.factoryTestSample.domain.FactoryTestSample;
import com.ruoyi.develop.factory.factoryTestSample.mapper.FactoryTestSampleMapper;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.mapper.GoodsSaleMapper;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import com.ruoyi.develop.lvYifahuomingxi.domain.LvYifahuomingxi;
import com.ruoyi.develop.lvYifahuomingxi.mapper.LvYifahuomingxiMapper;
import com.ruoyi.develop.project.domain.Project;
import com.ruoyi.develop.project.mapper.ProjectMapper;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.mapper.ProjectBaseInfoMapper;
import com.ruoyi.export.service.IExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.factory.factoryTestMachineryLink.mapper.FactoryTestMachineryLinkMapper;
import com.ruoyi.develop.factory.factoryTestMachineryLink.domain.FactoryTestMachineryLink;
import com.ruoyi.develop.factory.factoryTestMachineryLink.service.IFactoryTestMachineryLinkService;

/**
 * 出厂试验-机械试验1Service业务层处理
 *
 * @author lmm
 * @date 2024-11-05
 */
@Service
public class FactoryTestMachineryLinkServiceImpl implements IFactoryTestMachineryLinkService {
    @Resource
    private FactoryTestMachineryLinkMapper factoryTestMachineryLinkMapper;
    @Resource
    private FactoryProduceWorkMapper factoryProduceWorkMapper;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    @Resource
    private GoodsSaleMapper goodsSaleMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private ProjectBaseInfoMapper projectBaseInfoMapper;
    @Resource
    private LvDaohuoDetailMapper lvDaohuoDetailMapper;

    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;

    @Resource
    private FactoryTestSampleMapper factoryTestSampleMapper;

    @Resource
    private LvYifahuomingxiMapper lvYifahuomingxiMapper;
    @Autowired
    private IExportService exportService;




    /**
     * 查询出厂试验-机械试验1
     *
     * @param id 出厂试验-机械试验1主键
     * @return 出厂试验-机械试验1
     */
    @Override
    public FactoryTestMachineryLink selectById(String id) {
        return factoryTestMachineryLinkMapper.selectById(id);
    }

    /**
     * 查询出厂试验-机械试验1列表
     *
     * @param entity 出厂试验-机械试验1
     * @return 出厂试验-机械试验1
     */
    @Override
    public List<FactoryTestMachineryLink> selectList(FactoryTestMachineryLink entity) {
        return factoryTestMachineryLinkMapper.selectList(entity);
    }

    @Override
    public List<FactoryTestMachineryLink> selectListByFahuoDetail(FactoryTestMachineryLink entity) {
        return factoryTestMachineryLinkMapper.selectListByFahuoDetail(entity);
    }



    /**
     * 新增出厂试验-机械试验1
     *
     * @param entity 出厂试验-机械试验1
     * @return 结果
     */
    @Override
    public int insert(FactoryTestMachineryLink entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if(StringUtils.isNotBlank(entity.getWorkOrderId())) {
            FactoryProduceWork workOrder =  factoryProduceWorkMapper.selectById(entity.getOrderId());
            if (StringUtils.isNotBlank(workOrder.getProNo())) {
                Project project = projectMapper.selectProjectByCode(workOrder.getProNo());
                entity.setProjectId(project.getId());
                entity.setProjectCode(project.getCode());
            }
            if (StringUtils.isNotBlank(workOrder.getMaterialsCode())) {
                entity.setGoodsId(workOrder.getMaterialsCode());
                GoodsOrderIn goods = goodsOrderInMapper.selectByCode(workOrder.getMaterialsCode());
                entity.setGoodsId(goods.getId());
                entity.setGoodsName(goods.getName());
                entity.setGoodsModel(goods.getModel());
            }
        }

        if(StringUtils.isNotBlank(entity.getDaohuoDetailId())) {
            LvDaohuoDetail lvDaohuoDetail = lvDaohuoDetailMapper.selectById(entity.getDaohuoDetailId());
            if(lvDaohuoDetail!=null && StringUtils.isNotBlank(lvDaohuoDetail.getProjectId())){
                ProjectBaseInfo order = projectBaseInfoMapper.selectById(lvDaohuoDetail.getProjectId());
                entity.setProjectId(order.getProject().getId());
                entity.setProjectCode(order.getProject().getCode());
            }
            GoodsOrderIn goods = goodsOrderInMapper.selectById(lvDaohuoDetail.getGoodId());
            entity.setGoodsId(goods.getId());
            entity.setGoodsName(goods.getName());
            entity.setGoodsModel(goods.getModel());
            entity.setGoodsSaleId(lvDaohuoDetail.getGoodsSaleId());
        }

        if(StringUtils.isNotBlank(entity.getRukuId())) {
            LvRukumingxi lvRukumingxi = lvRukumingxiMapper.selectById(entity.getRukuId());
            if(lvRukumingxi!=null && StringUtils.isNotBlank(lvRukumingxi.getProjectId())){
                ProjectBaseInfo order = projectBaseInfoMapper.selectById(lvRukumingxi.getProjectId());
                entity.setProjectId(order.getProject().getId());
                entity.setProjectCode(order.getProject().getCode());
            }
            GoodsOrderIn goods = goodsOrderInMapper.selectById(lvRukumingxi.getGoodsId());
            entity.setGoodsId(goods.getId());
            entity.setGoodsName(goods.getName());
            entity.setGoodsModel(goods.getModel());
            entity.setGoodsSaleId(lvRukumingxi.getGoodsSaleId());
        }

        if(StringUtils.isNotBlank(entity.getGoodsSaleId())) {
            GoodsSale goodsSale = goodsSaleMapper.selectById(entity.getGoodsSaleId());
            if(goodsSale!=null && StringUtils.isNotBlank(goodsSale.getProjectId())){
                ProjectBaseInfo order = projectBaseInfoMapper.selectById(goodsSale.getProjectId());
                entity.setProjectId(order.getProject().getId());
                entity.setProjectCode(order.getProject().getCode());
            }
            if(goodsSale!=null && StringUtils.isNotBlank(goodsSale.getGoodsId())){
                GoodsOrderIn goods = goodsOrderInMapper.selectById(goodsSale.getGoodsId());
                entity.setGoodsId(goods.getId());
                entity.setGoodsName(goods.getName());
                entity.setGoodsModel(goods.getModel());
            }
        }

        if(StringUtils.isNotBlank(entity.getSampleId())) {
            FactoryTestSample sample = factoryTestSampleMapper.selectById(entity.getSampleId());
            if(StringUtils.isNotBlank(sample.getGoodsId())) {
                GoodsOrderIn goods = goodsOrderInMapper.selectById(sample.getGoodsId());
                entity.setGoodsId(goods.getId());
                entity.setGoodsName(goods.getName());
                entity.setGoodsModel(goods.getModel());
            }
        }

        if(StringUtils.isNotBlank(entity.getFahuoDetailId())) {
            LvYifahuomingxi lvYifahuomingxi = lvYifahuomingxiMapper.selectById(entity.getFahuoDetailId());
            entity.setGoodsId(lvYifahuomingxi.getGoodsId());
            entity.setGoodsName(lvYifahuomingxi.getName());
            entity.setGoodsModel(lvYifahuomingxi.getModels());
        }

        if(StringUtils.isBlank(entity.getBatchNumber())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String date = sdf.format(entity.getInspectTime()!=null?entity.getInspectTime():new Date());
            String codeMax = factoryTestMachineryLinkMapper.selectMaxBatchNumber(date);
            if(StringUtils.isNotBlank(codeMax)){
                // 最大编号+1
                codeMax = Long.parseLong(codeMax)+1+"";
            }else{
                codeMax = date+"0001";
            }
            entity.setBatchNumber(codeMax);
        }


        if(StringUtils.isBlank(entity.getSampleNumber())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String date = sdf.format(entity.getInspectTime()!=null?entity.getInspectTime():new Date());
            String codeMax = factoryTestMachineryLinkMapper.selectMaxSampleNumber(date);
            if(StringUtils.isNotBlank(codeMax)){
                // 最大编号+1
                codeMax = Long.parseLong(codeMax)+1+"";
            }else{
                codeMax = date+"0001";
            }
            entity.setSampleNumber(codeMax);
        }

        generateInspectReport(entity);
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return factoryTestMachineryLinkMapper.insert(entity);
    }

    /**
     * 修改出厂试验-机械试验1
     *
     * @param entity 出厂试验-机械试验1
     * @return 结果
     */
    @Override
    public int update(FactoryTestMachineryLink entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        generateInspectReport(entity);
        return factoryTestMachineryLinkMapper.update(entity);
    }

    /**
     * 批量删除出厂试验-机械试验1
     *
     * @param ids 需要删除的出厂试验-机械试验1主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return factoryTestMachineryLinkMapper.deleteByIds(ids);
    }

    /**
     * 删除出厂试验-机械试验1信息
     *
     * @param id 出厂试验-机械试验1主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return factoryTestMachineryLinkMapper.deleteById(id);
    }

    public void updateRukumingxiByDaohuo(String daohuoDetailId, String rukuMingxiId) {
        factoryTestMachineryLinkMapper.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
    }


    /** 根据质检数据生成质检报告 */
    public void generateInspectReport(FactoryTestMachineryLink entity){
        Map<String, Object> params = new HashMap<>();
        params.put("entity", entity);
        params.put("inspectDate", DateUtils.formatDate(entity.getInspectTime(),"yyyy年MM月dd日"));
        String fileName = "机械试验（悬垂线夹和连接金具）检验报告.docx";
        String templateName = "机械试验（悬垂线夹和连接金具）检验报告.docx";
        String url = exportService.convertDocxAndUpload(fileName, params, templateName);
        entity.setReportFile(url);
    }
}
